package com.talent.jiwen.http.common;

import rx.Observable;
import rx.functions.Action0;
import rx.subjects.PublishSubject;

/**
 * @date 创建时间：2017/8/5
 * @author cmw
 * @description
 */

public class HttpUtil {

    /**
     * 构造方法私有
     */
    private HttpUtil() {}

    /**
     * 在访问HttpMethods时创建单例
     */
    private static class SingletonHolder {
        private static final HttpUtil INSTANCE = new HttpUtil();
    }

    /**
     * 获取单例
     */
    public static HttpUtil getInstance() {
        return SingletonHolder.INSTANCE;
    }
    /**
     * 添加线程管理并订阅
     * @param ob
     * @param subscriber
     * @param event            Activity 生命周期
     * @param lifecycleSubject
     * @param forceRefresh     是否强制刷新
     *                         是否显示dialog
     */
    public void toSubscribe(Observable ob, final ProgressSubscriber subscriber,final ActivityLifeCycleEvent event, final PublishSubject<ActivityLifeCycleEvent> lifecycleSubject, boolean forceRefresh, final boolean isShowDia) {
        //数据预处理
        Observable.Transformer<HttpResult<Object>, Object> result = RxHelper.handleResult(event, lifecycleSubject);
        Observable observable = ob.compose(result)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        //显示Dialog和一些其他操作
                        if (isShowDia) {
                            subscriber.showProgressDialog();
                        }
                    }
                });
        RetrofitCache.load( observable,forceRefresh).subscribe(subscriber);
    }


    /**
     * 无强制刷新下使用
     * @param ob
     * @param subscriber
     * @param event
     * @param lifecycleSubject
     * @param isShowDia
     */

    public void toSubscribe(Observable ob, final ProgressSubscriber subscriber, final ActivityLifeCycleEvent event, final PublishSubject<ActivityLifeCycleEvent> lifecycleSubject, final boolean isShowDia) {
        //数据预处理
        Observable.Transformer<HttpResult<Object>, Object> result = RxHelper.handleResult(event, lifecycleSubject);
        Observable observable = ob.compose(result)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        //显示Dialog和一些其他操作
                        if (isShowDia) {
                            subscriber.showProgressDialog();
                        }
                    }
                });
        observable.subscribe(subscriber);
    }


    /**
     *无activity生命周期下调用
     * @param ob
     * @param subscriber
     * @param isShowDia
     */

    public void toSubscribe(Observable ob, final ProgressSubscriber subscriber, final boolean isShowDia) {
        //数据预处理
        Observable.Transformer<HttpResult<Object>, Object> result = RxHelper.handleResult1();
        Observable observable = ob.compose(result)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        //显示Dialog和一些其他操作
                        if (isShowDia) {
                            subscriber.showProgressDialog();
                        }

                    }
                });
        observable.subscribe(subscriber);
    }

}
